package miniworld;

import homeMadeExceptions.NotYetImplementedException;

import java.util.HashMap;
import java.util.Vector;

import datatypes.BluetoothDeviceAddress;
import datatypes.Host;
import datatypes.Message;

public class LocalSimulationQueue {
	
	private Host locHost = null;
	private HashMap<Integer, Vector<Object>> parameter_Map = new HashMap<Integer, Vector<Object>>();
	private Vector<_call_method> actions = new Vector<_call_method>();
	
	public enum _call_method { BLUEZ_RFCOMM_RECV, BLUEZ_ONCLIENTCONNECTED, BLUEZ_INVOKE_MESSAGE_LOST, BLUEZ_INVOKEACKSIGNALFORSENDEDMESSAGE, BLUEZ_INVOKERFCOMMCONNECTREPLY , 
							   ROUTING_NOTIFICATION, DISCOVERY_NOTIFICATION,
							   DISCOVERY_SERVICE_ENABLE, DISCOVERY_SERVICE_DISABLE, DISCOVERY_ASK_HOST_FOR_SERVICES, DISCOVERY_FIND_SPECIFIC_SERVICE, DISCOVERY_GET_ALL_SERVICES, 
							   DISCOVERY_PUB_LOCAL_SERVICE, DISCOVERY_UPDATE_LOCAL_SERVICE, DISCOVERY_DELETE_LOCAL_SERVICE};

	public LocalSimulationQueue (Host host){
		this.locHost = host;
	}
	
	public void add_method_call (_call_method call_target, Vector<Object> parameterlist){
		actions.add(call_target);
		parameter_Map.put(actions.size()-1, parameterlist);
	}
	
	public void execute (){
		for (int i = 0 ; i < actions.size(); i++){
			this.execute_Single(actions.get(i), parameter_Map.get(i));		
		}
		actions.removeAllElements();
		parameter_Map.clear();
	}
	
	private void execute_Single (_call_method call_target, Vector<Object> parameterlist){
		switch (call_target){
		case BLUEZ_RFCOMM_RECV:
			this.locHost.getRoutingSocket().rfcomm_recv((Message)parameterlist.get(0), (BluetoothDeviceAddress)parameterlist.get(1));
			break;
		case BLUEZ_ONCLIENTCONNECTED:
			this.locHost.getRoutingSocket().onClientConnected((BluetoothDeviceAddress)parameterlist.get(0));
			break;
		case BLUEZ_INVOKE_MESSAGE_LOST:
			this.locHost.getRoutingSocket().invokeMessageLost((BluetoothDeviceAddress)parameterlist.get(0), (Message)parameterlist.get(1));
			break;
		case BLUEZ_INVOKEACKSIGNALFORSENDEDMESSAGE:
			this.locHost.getRoutingSocket().invokeAckSignalForSendedMessage((Message)parameterlist.get(0), (BluetoothDeviceAddress)parameterlist.get(1));
			break;
		case BLUEZ_INVOKERFCOMMCONNECTREPLY:
			this.locHost.getRoutingSocket().invokeRfcommConnectReply((BluetoothDeviceAddress) parameterlist.get(0), ((Boolean)parameterlist.get(1)).booleanValue());
			break;
		case ROUTING_NOTIFICATION:
			this.locHost.getRoutingSocket().notificationAPI();
			break;
		case DISCOVERY_NOTIFICATION:
			this.locHost.getDiscoverySocket().notificationAPI();
			break;
		case DISCOVERY_PUB_LOCAL_SERVICE:
			try {
					this.locHost.getDiscoverySocket().publishLocalServices((String) parameterlist.get(0));
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		case DISCOVERY_UPDATE_LOCAL_SERVICE:
			try {
					this.locHost.getDiscoverySocket().updateLocalService((String) parameterlist.get(0), (String)parameterlist.get(1));
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		case DISCOVERY_DELETE_LOCAL_SERVICE:
			try {
					this.locHost.getDiscoverySocket().deleteLocalService((String)parameterlist.get(0));
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		case DISCOVERY_SERVICE_DISABLE:
			this.locHost.getDiscoverySocket().disableDiscoveryMechanism();
			break;
		case DISCOVERY_SERVICE_ENABLE:
			this.locHost.getDiscoverySocket().enableDiscoveryMechanism();
			break;
		case DISCOVERY_ASK_HOST_FOR_SERVICES:
			try {
					this.locHost.getDiscoverySocket().askHostForServices((BluetoothDeviceAddress) parameterlist.get(0));
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		case DISCOVERY_FIND_SPECIFIC_SERVICE:
			try {
					this.locHost.getDiscoverySocket().findSpecificService((String)parameterlist.get(0));
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		case DISCOVERY_GET_ALL_SERVICES:
			try {
					this.locHost.getDiscoverySocket().getAllAvailableServices();
				} catch (NotYetImplementedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
		}
	}
}